home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / pbmtext.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  5.3 KB  |  215 lines

  1. /* pbmtext.c - render text into a bitmap
  2. **
  3. ** Copyright (C) 1991 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14. #include "pbmfont.h"
  15.  
  16. static void fix_control_chars ARGS(( char* buf ));
  17. static void fill_rect ARGS(( bit** bits, int row0, int col0, int height, int width, bit color ));
  18. static void copy_rect ARGS(( bit** fbits, int frow0, int fcol0, int height, int width, bit** tbits, int trow0, int tcol0 ));
  19.  
  20. void
  21. main( argc, argv )
  22.     int argc;
  23.     char* argv[];
  24.     {
  25.     bit** bits;
  26.     int argn, rows, cols, row, col;
  27.     bit** font;
  28.     int frows, fcols;
  29.     FILE* ifp;
  30.     int dump;
  31.     int char_width, char_height, char_awidth, char_aheight, vmargin, hmargin;
  32.     int char_row0[95];
  33.     int char_col0[95];
  34.     char buf[5000];
  35.     char** lp;
  36.     int lines, maxlines, maxchars, line;
  37.     char* cp;
  38.     char* usage = "[-font <fontfile>] [text]";
  39.  
  40.     pbm_init( &argc, argv );
  41.  
  42.     /* Set up default parameters. */
  43.     argn = 1;
  44.     font = pbm_defaultfont( &fcols, &frows );
  45.     dump = 0;
  46.  
  47.     /* Check for flags. */
  48.     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  49.     {
  50.     if ( pm_keymatch( argv[argn], "-font", 2 ) )
  51.         {
  52.         ++argn;
  53.         if ( argn == argc )
  54.         pm_usage( usage );
  55.         ifp = pm_openr( argv[argn] );
  56.         pbm_freearray( font, frows );
  57.         font = pbm_readpbm( ifp, &fcols, &frows );
  58.         pm_close( ifp );
  59.         }
  60.     else if ( pm_keymatch( argv[argn], "-dump", 2 ) )
  61.         /* Undocumented dump flag for installing a new built-in font. */
  62.         dump = 1;
  63.     else
  64.         pm_usage( usage );
  65.     ++argn;
  66.     }
  67.  
  68.     if ( dump )
  69.     {
  70.     pbm_dumpfont( font, fcols, frows );
  71.     exit( 0 );
  72.     }
  73.  
  74.     maxlines = 50;
  75.     lp = (char**) malloc( maxlines * sizeof(char*) );
  76.     if ( lp == (char**) 0 )
  77.     pm_error( "out of memory" );
  78.  
  79.     if ( argn < argc )
  80.     { /* Get text from the command line. */
  81.     (void) strcpy( buf, argv[argn] );
  82.     ++argn;
  83.     while ( argn < argc )
  84.         {
  85.         (void) strcat( buf, " " );
  86.         (void) strcat( buf, argv[argn] );
  87.         ++argn;
  88.         }
  89.     fix_control_chars( buf );
  90.     maxchars = strlen( buf );
  91.     lp[0] = buf;
  92.     lines = 1;
  93.     }
  94.     else
  95.     { /* Read text from stdin. */
  96.     lines = 0;
  97.     maxchars = 0;
  98.     while ( gets( buf ) != NULL )
  99.         {
  100.         int l;
  101.  
  102.         fix_control_chars( buf );
  103.         l = strlen( buf );
  104.         maxchars = max( maxchars, l );
  105.         if ( lines >= maxlines )
  106.         {
  107.         maxlines *= 2;
  108.         lp = (char**) realloc( (char*) lp, maxlines * sizeof(char*) );
  109.         if ( lp == (char**) 0 )
  110.             pm_error( "out of memory" );
  111.         }
  112.         lp[lines] = (char*) malloc( l + 1 );
  113.         if ( lp[lines] == 0 )
  114.         pm_error( "out of memory" );
  115.         (void) strcpy( lp[lines], buf );
  116.         ++lines;
  117.         }
  118.     }
  119.  
  120.     pbm_dissectfont( font, frows, fcols, &char_height, &char_width,
  121.              &char_aheight, &char_awidth, char_row0, char_col0 );
  122.     if ( lines == 1 )
  123.     {
  124.     vmargin = char_height / 2;
  125.     hmargin = char_width;
  126.     }
  127.     else
  128.     {
  129.     vmargin = char_height;
  130.     hmargin = 2 * char_width;
  131.     }
  132.     rows = 2 * vmargin + lines * char_height;
  133.     cols = 2 * hmargin + maxchars * char_width;
  134.     bits = pbm_allocarray( cols, rows );
  135.  
  136.     /* Fill background with the color of ' '. */
  137.     fill_rect( bits, 0, 0, rows, cols, font[char_row0[0]][char_col0[0]] );
  138.     
  139.     /* Render characters. */
  140.     for ( line = 0; line < lines; ++line )
  141.     {
  142.     row = vmargin + line * char_height;
  143.     col = hmargin;
  144.  
  145.     for ( cp = lp[line]; *cp != '\0'; ++cp )
  146.         {
  147.         copy_rect( font, char_row0[*cp - ' '], char_col0[*cp - ' '],
  148.                char_height, char_width, bits, row, col );
  149.         col += char_width;
  150.         }
  151.     }
  152.  
  153.     /* All done. */
  154.     pbm_writepbm( stdout, bits, cols, rows, 0 );
  155.     pm_close( stdout );
  156.  
  157.     exit( 0 );
  158.     }
  159.  
  160. static void
  161. fix_control_chars( buf )
  162.     char* buf;
  163.     {
  164.     int i, j, n, l;
  165.  
  166.     for ( i = 0; buf[i] != '\0'; ++i )
  167.     {
  168.     if ( buf[i] == '\t' )
  169.         { /* Turn tabs into the right number of spaces. */
  170.         n = ( i + 8 ) / 8 * 8;
  171.         l = strlen( buf );
  172.         for ( j = l; j > i; --j )
  173.         buf[j + n - i - 1] = buf[j];
  174.         for ( ; i < n; ++i )
  175.         buf[i] = ' ';
  176.         --i;
  177.         }
  178.     else if ( buf[i] < ' ' || buf[i] > '~' )
  179.         /* Turn other control chars into a single space. */
  180.         buf[i] = ' ';
  181.     }
  182.     }
  183.  
  184. #if __STDC__
  185. static void
  186. fill_rect( bit** bits, int row0, int col0, int height, int width, bit color )
  187. #else /*__STDC__*/
  188. static void
  189. fill_rect( bits, row0, col0, height, width, color )
  190.     bit** bits;
  191.     int row0, col0, height, width;
  192.     bit color;
  193. #endif /*__STDC__*/
  194.     {
  195.     int row, col;
  196.  
  197.     for ( row = row0; row < row0 + height; ++row )
  198.     for ( col = col0; col < col0 + width; ++col )
  199.         bits[row][col] = color;
  200.     }
  201.  
  202. static void
  203. copy_rect( fbits, frow0, fcol0, height, width, tbits, trow0, tcol0 )
  204.     bit** fbits;
  205.     int frow0, fcol0, height, width;
  206.     bit** tbits;
  207.     int trow0, tcol0;
  208.     {
  209.     int row, col;
  210.  
  211.     for ( row = 0; row < height; ++row )
  212.     for ( col = 0; col < width; ++col )
  213.         tbits[trow0 + row][tcol0 + col] = fbits[frow0 + row][fcol0 + col];
  214.     }
  215.